'use strict';
app.service('accountService',['$http','$q','utilService',function($http,$q,utilService){

    this.createSystemUser = function($scope){

        var roleIds=new Array();
        for(var i=0;i<$scope.roles.length;i++){
            if($scope.roles[i].isSelect==1) {
                roleIds.push($scope.roles[i].id);
            }
        }

        $scope.systemUser.roleIds=roleIds;

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'system/user',
            data:$scope.systemUser
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.findById = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'system/user/'+ $scope.systemUser.systemUserId,
            params:{systemUserId:$scope.systemUser.systemUserId}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }

    this.updateSystemUser = function($scope){
        var roleIds=new Array();
        for(var i=0;i<$scope.roles.length;i++){
            if($scope.roles[i].isSelect==1) {
                roleIds.push($scope.roles[i].id);
            }
        }
        $scope.systemUser.roleIds=roleIds;

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'system/user/'+ $scope.systemUser.id,
            data:$scope.systemUser,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.getAll = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'role/all/function',
            params:$scope.role
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错！");
        });
        return deferred.promise;
    }


    this.updateSystemUserStatus=function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + '/system/user/status/'+ $scope.systemUserId,
            data:{status:$scope.status},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;

    }



}]);